package com.polaris.live.viewmodel.me

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.PartyRoomResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import com.polaris.live.ui.main.live.FriendRoomFragment
import com.polaris.live.widget.refresh.OnListListener

/**
 * FriendRoomViewModel
 *
 * @author Created by 半仙 on 2024/9/21/021 11:57
 */
class FriendRoomViewModel : BaseViewModel(), OnListListener {

    val mListLiveData = MutableLiveData<ResultState<ListResp<Any>>>()
    var mRequestType = FriendRoomFragment.FOLLOWER

    private fun getRoomPage(position: Int) {
        when (mRequestType) {
            FriendRoomFragment.FOLLOWER -> {

                request({ homeService.getRoomFollowed(position).let { it.new(it.result?.list) } }, mListLiveData)
            }

            FriendRoomFragment.NEARBY -> {

                request({ homeService.getRoomRecently() }, {
                    mListLiveData.value = ResultState.onAppSuccess(ListResp(false, it, 0, 0, 0, 0))
                }, {
                    mListLiveData.value = ResultState.onAppError(it)
                })
            }

            FriendRoomFragment.JOINED -> {
                request({ homeService.getRoomJoined(position).let { it.new(it.result?.list) } }, mListLiveData)

            }
        }
    }

    override fun onRefresh() {
        getRoomPage(0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getRoomPage(pageIndex)
    }

    val mPartyRoomLiveData = MutableLiveData<ResultState<PartyRoomResp>>()
    fun getPartyRoom() {
        request({ liveService.getPartyRoom() }, mPartyRoomLiveData)
    }
}